//
//  MonthButton.m
//  仿系统日历
//
//  Created by 王猛 on 2022/11/16.
//

#import "MonthButton.h"
#import "UIColor+WM.h"

#define COLORHEX(hex) [UIColor colorWithHexString:hex]
/***  当前屏幕宽度 */
#define kScreenWidth  [[UIScreen mainScreen] bounds].size.width
/***  当前屏幕高度 */
#define kScreenHeight  [[UIScreen mainScreen] bounds].size.height
@implementation MonthButton

- (instancetype)initWithFrame:(CGRect)frame withMonthModel:(MonthModel *)monthModel {
    if (self = [super initWithFrame:frame]) {
        self.monthModel = monthModel;
        [self setUI];
    }
    return self;
}

- (void)setUI {
    for (UIView *v in self.subviews) {
        [v removeFromSuperview];
    }
    
    UILabel *monthLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 20)];
    monthLab.textColor = COLORHEX(@"000000");
    monthLab.textAlignment = NSTextAlignmentLeft;
    monthLab.font = [UIFont systemFontOfSize:16];
    monthLab.text = [NSString stringWithFormat:@"%ld月",self.monthModel.month];
    [self addSubview:monthLab];

    DayModel *firstDay = self.monthModel.dayList.firstObject;
    NSInteger firstIndex = firstDay.date.weekday - 1;
    NSInteger HN = 7;
    NSInteger VN = 6;
    CGFloat BW = self.frame.size.width/HN;
    CGFloat BH = (self.frame.size.height - CGRectGetMaxY(monthLab.frame))/VN;
    for (int index = 0; index < 42; index++) {
        NSInteger modelIndex = index - firstIndex;
        if (modelIndex < self.monthModel.dayList.count && modelIndex >= 0) {
            DayModel *dayModel = self.monthModel.dayList[modelIndex];
            UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(BW*(index%HN),CGRectGetMaxY(monthLab.frame) + BH*(index/HN), BW, BH)];
            view1.backgroundColor = [UIColor clearColor];
            view1.userInteractionEnabled = NO;
            [self addSubview:view1];
            
            NSString *text = [NSString stringWithFormat:@"%ld",(long)dayModel.day];
            UILabel *lab = [[UILabel alloc] initWithFrame:view1.bounds];
            lab.text = text;
            lab.textAlignment = NSTextAlignmentCenter;
            lab.font = [UIFont systemFontOfSize:10];
            lab.textColor = COLORHEX(@"000000");
            [view1 addSubview:lab];
            
            NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:[NSDate date]];
            
            if (dayModel.date.year == components.year && dayModel.date.month == components.month && dayModel.date.day == components.day) {
                CGFloat size = BW;
                if (BW > BH) {
                    size = BH;
                }
                UIView *pointView = [[UIView alloc] initWithFrame:CGRectMake((view1.frame.size.width - size)/2, (view1.frame.size.height - size)/2, size, size)];
                pointView.layer.cornerRadius = size/2;
                pointView.layer.masksToBounds = YES;
                pointView.backgroundColor = COLORHEX(@"888888");
                [view1 insertSubview:pointView atIndex:0];
                lab.textColor = [UIColor whiteColor];
            }
    
        }
        
    }
}

@end
